from pymongo import MongoClient
import pymongo.collection
import urllib.parse
import json
import os

# _Ip='10.222.2.18'
# _Port=27017
# _UserName='gxw'
# _Password='GxW1996623'
# _Database='schoolweb'
# _AuthMechanism='SCRAM-SHA-1'

# class MongoConnector:
#     __ConfigName='MongoConnector'
#     def __init__(self,host,port,username,password,author_source,auth_mechanism):
#         super(MongoConnector,self).__init__(MongoConnector.__ConfigName)
#         self.__connection=MongoClient(host=host,port=port,username=username,password=password,authSource=author_source,authMechanism=auth_mechanism)
#         #self.__connection=MongoClient(host='120.202.26.167',port=27017,username='gxw',password='GxW1996623',authSource='schoolweb',authMechanism=_AuthMechanism)

#     def dbInstance(self,dbName):
#         return MongoDbAdapter(self.__connection.get_database(dbName),dbName)

#     def close(self):
#         self.__connection.close()
        
# class MongoDbAdapter:
#     def __init__(self,db,dbName):
#         self.DbName=dbName
#         self.__obj=db
#     def collection(self,collectionName):
#         collection=self.__obj.get_collection(collectionName)
#         return MongoCollectionAdapter(collection,collectionName)

#     def hasCollection(self,collectionName):
#         pass

#     def collectionNames(self):
#         return self.__obj.collection_names()

#     def drop(self,name):
#         self.__obj.drop_collection(name)
#         return name

class MongoTable:
    def __init__(self,client:pymongo.MongoClient,database_name,table_name):
        self.client=client
        self.__obj=client.get_database(database_name).get_collection(table_name)
    
    def selectAll(self):
        return self.__obj.find()

    def selectWhere(self,limit):
        return self.__obj.find(limit)

    def selectFirst(self,limit):
        return self.__obj.find_one(limit)

    def insert(self,doc):
        self.__obj.insert(doc)
    
    def insertMany(self,docs):
        self.__obj.insert_many(docs)

    def remove(self,limit):
        self.__obj.delete_many(limit)
    
    def removeOne(self,limit):
        self.__obj.delete_one(limit)

    def update(self,limit,newDoc):
        self.__obj.update(limit,{"$set":newDoc})

    def replace(self,limit,newDoc):
        self.__obj.replace_one(limit,newDoc,upsert=True)

    def count(self,limit={}):
        return self.__obj.count_documents(limit)

    def createIndex(self,columns,background=False,unique=False):
        self.__obj.create_index(columns,background=background,unique=False)

    def dropIndex(self,word):
        self.__obj.drop_index(word)

    def distinct(self,limit):
        return self.__obj.distinct(limit)

    def indexs(self):
        return self.__obj.list_indexes()

    def drop(self):
        self.__obj.drop()

if __name__=='__main__':
    pass